package com.sy.simpleegg.mvp.presenter

import androidx.lifecycle.LifecycleCoroutineScope
import com.sy.simpleegg.base.BasePresenter
import com.sy.simpleegg.bean.CashGoodBean
import com.sy.simpleegg.bean.CheckSaveCoinsResultBean
import com.sy.simpleegg.bean.GoodBean
import com.sy.simpleegg.bean.GoodListBean
import com.sy.simpleegg.bean.LocalWithdrawalOrderBean
import com.sy.simpleegg.bean.OrderDataBean
import com.sy.simpleegg.bean.PayTypeData
import com.sy.simpleegg.bean.RepCashDataBean
import com.sy.simpleegg.bean.RepCashYch
import com.sy.simpleegg.platform.lyysdk.bean.RecieveOrderLyySdkBean
import com.sy.simpleegg.bean.ReqBackscanPayBean
import com.sy.simpleegg.bean.ReqPayCodeBean
import com.sy.simpleegg.constant.OutCoinState
import com.sy.simpleegg.constant.PackageType
import com.sy.simpleegg.constant.PayState
import com.sy.simpleegg.ext.getDeviceStatus
import com.sy.simpleegg.ext.logFile
import com.sy.simpleegg.ext.setDeviceStatus
import com.sy.simpleegg.mvp.contract.HomeContract
import com.sy.simpleegg.mvp.model.HomeModel
import com.sy.simpleegg.platform.BackstagePlatform
import com.sy.simpleegg.platform.kkb.bean.RecieveOrderKkbBean
import com.sy.simpleegg.platform.lyybox.bean.RecieveOrderLyyBoxBean
import com.sy.simpleegg.platform.whmhex.bean.RecieveOrderWhmMqttHexBean
import com.sy.simpleegg.platform.whmhex.bean.RecieveWithdrawalCoinOrderWhmMqttHexBean
import com.sy.simpleegg.platform.ychsdk.bean.RecieveOrderYchBean
import com.sy.simpleegg.room.RoomUtil
import com.sy.simpleegg.room.entity.OrderEntity
import com.sy.simpleegg.util.CommonUtil
import com.sy.simpleegg.util.DateTimeUtil
import com.sy.simpleegg.util.GsonUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Queue
import java.util.concurrent.LinkedBlockingDeque

class HomePresenter(private val lifecycleCoroutineScope: LifecycleCoroutineScope) :
    BasePresenter<HomeContract.Model, HomeContract.View>(), HomeContract.Presenter {

    private val orderQueue: Queue<OrderDataBean> = LinkedBlockingDeque<OrderDataBean>()
    private var handleJob: Job? = null
    private var outCoinsState = OutCoinState.UNSEND//0-初始状态 1-出币中 2-出币结束 3-故障
    private var isLoop = true
    private var coinsResponTimeout = 30
    private var currentOutCoins = "0"

    private var isHandling = false
    private var isSaving = false

    override fun createModel(): HomeContract.Model = HomeModel()
    override fun initHandleJob() {
        handleJob?.cancel()

        handleJob = lifecycleCoroutineScope.launch(Dispatchers.IO) {
            while (isLoop) {
                try {
                    run checkSize@{
                        if (orderQueue.size > 0) {
                            var bean: OrderDataBean? = null
                            while (!isHandling && getDeviceStatus() && orderQueue.poll().also { bean = it } != null) {
                                logFile("订单开始处理==========")
                                isHandling = true
                                currentOutCoins = "0"
                                val p = bean!!.platform
                                val extRepData = bean!!.extRepData
                                val orderData = bean!!.orderData
                                var k = ""
                                var orderNumber = ""
                                var unique = ""
                                var money = ""
                                var coins = ""
                                var isCash = false
                                val isWithdrawal = bean!!.isWithdrawal
                                var isMqOrderYch = true
                                var isWriteOff = false
                                var uid = bean!!.uid
                                var isSave = bean!!.isSave

                                //存币订单处理，等待存币串口关闭（存币结束）
                                isSaving = isSave
                                var saveTimeout  = 60
                                while (isSave && isSaving){
                                    delay(1000)
                                }
                                if (isSave) {
                                    isHandling = false
                                    delay(1000)
                                    logFile("订单处理结束:存币结束==========")
                                    return@checkSize
                                }

                                //解析订单数据
                                when (p){
                                    BackstagePlatform.DEF_MQTT -> {

                                    }
                                    BackstagePlatform.LYY_SDK -> {
                                        val orderLyySdk = orderData as RecieveOrderLyySdkBean
                                        k = orderLyySdk.k
                                        unique = orderLyySdk.unique
                                        money = orderLyySdk.money
                                        coins = orderLyySdk.coins
                                        isCash = orderLyySdk.isCash
                                    }

                                    BackstagePlatform.LYY_BOX -> {
                                        val orderLyyBox = orderData as RecieveOrderLyyBoxBean
                                        k = orderLyyBox.k6
                                        unique = orderLyyBox.k6
                                        money = orderLyyBox.money
                                        coins = orderLyyBox.coins
                                    }

                                    BackstagePlatform.WHM_MQTT -> {

                                    }

                                    BackstagePlatform.WHM_MQTT_HEX -> {
                                        if (isWithdrawal){
                                            val orderWhmMqttHex = orderData as RecieveWithdrawalCoinOrderWhmMqttHexBean
                                            k = orderWhmMqttHex.k
                                            unique = orderWhmMqttHex.unique
                                            money = orderWhmMqttHex.money
                                            coins = orderWhmMqttHex.coins
                                            isCash = orderWhmMqttHex.isCash
                                        }else{
                                            val orderWhmMqttHex = orderData as RecieveOrderWhmMqttHexBean
                                            k = orderWhmMqttHex.k
                                            unique = orderWhmMqttHex.unique
                                            money = orderWhmMqttHex.money
                                            coins = orderWhmMqttHex.coins
                                            isCash = orderWhmMqttHex.isCash
                                        }

                                        logFile("whmhex订单数据：k=$k unique=$unique money=$money coins=$coins isCash=$isCash isWithdrawal=$isWithdrawal")
                                    }

                                    BackstagePlatform.YCH_SDK -> {
                                        val orderYch = orderData as RecieveOrderYchBean
                                        k = orderYch.orderId
                                        unique = orderYch.orderId
                                        orderNumber = orderYch.orderNumber
                                        money = orderYch.money
                                        coins = orderYch.coins
                                        isCash = orderYch.isCash
                                        isMqOrderYch = orderYch.isMqOrder
                                        isWriteOff = orderData.isWriteOff
                                    }

                                    BackstagePlatform.KKB -> {
                                        val orderKkb = orderData as RecieveOrderKkbBean
                                        k = orderKkb.orderId
                                        unique = orderKkb.orderId
                                        orderNumber = orderKkb.orderNumber
                                        money = orderKkb.money
                                        coins = orderKkb.coins
                                        isCash = orderKkb.isCash
                                        isMqOrderYch = orderKkb.isMqOrder
                                        isWriteOff = orderData.isWriteOff
                                    }
                                }

                                //校验订单
                                var targetOrder: OrderEntity? = null
                                when (p){
                                    BackstagePlatform.DEF_MQTT -> {
                                        //校验订单
                                        withContext(Dispatchers.IO){
//                                            targetOrder = mModel?.checkOrder(bean!!.k)
                                        }
                                    }
                                    BackstagePlatform.LYY_SDK -> {
                                        val lyySdkOrder = OrderEntity().apply {
                                            platform = p
                                            amount = coins
                                            this.money = money
                                            payUuid = k
                                            orderNumber = unique
                                            isPay = PayState.SUCCESS
                                        }

                                        withContext(Dispatchers.IO){
                                            targetOrder = mModel?.checkOrder(lyySdkOrder, isCash)
                                        }
                                    }

                                    BackstagePlatform.LYY_BOX -> {
                                        val boxOrder = OrderEntity().apply {
                                            platform = p
                                            amount = coins
                                            this.money = money
                                            uuid = k
                                            isPay = PayState.SUCCESS
                                            goodType = PackageType.RECHARGE
                                            date = DateTimeUtil.nowDate
                                        }
                                        mModel?.saveOrder(boxOrder)
                                        targetOrder = mModel?.getLastRechargeOrder(p)?.let {
                                                if (it.isEmpty()){
                                                    null
                                                }else{
                                                    it[0]
                                                }
                                        }
                                    }

                                    BackstagePlatform.WHM_MQTT -> {

                                    }

                                    BackstagePlatform.WHM_MQTT_HEX -> {
                                        val whmMqttHexOrder = OrderEntity().apply {
                                            uuid = k
                                            platform = p
                                            amount = coins
                                            this.money = money
                                            payUuid = k
                                            orderNumber = k
                                            isPay = PayState.SUCCESS
                                        }
                                        logFile("whmhex校验订单数据：${GsonUtil.getInstance()?.toJson(whmMqttHexOrder)}")
                                        withContext(Dispatchers.IO){
                                            if (isWithdrawal){
                                                targetOrder = mModel?.getWithdrawalOrder(k)
                                            }else{
                                                targetOrder = mModel?.checkOrderByCoins(whmMqttHexOrder, isCash)
                                            }
                                            logFile("whmhex校验后的订单数据：${GsonUtil.getInstance()?.toJson(targetOrder)}")
                                        }
                                    }

                                    BackstagePlatform.YCH_SDK -> {
                                        val ychOrder = OrderEntity().apply {
                                            uuid = k
                                            payUuid = k
                                            platform = p
                                            amount = coins
                                            this.money = money
                                            this.orderNumber = orderNumber
                                            isPay = PayState.SUCCESS
                                        }
                                        logFile("ych校验订单数据：${GsonUtil.getInstance()?.toJson(ychOrder)}")
                                        withContext(Dispatchers.IO){
                                            if (isMqOrderYch && !isWithdrawal && !isWriteOff){
                                                //普通订单
                                                logFile("ych 普通订单")
                                                mModel?.saveOrder(ychOrder)
                                                targetOrder = ychOrder
                                            }else if (isWithdrawal){
                                                //提币订单
                                                logFile("ych 提币订单")
                                                targetOrder = mModel?.getWithdrawalOrder(ychOrder.orderNumber)
                                            }else if (isWriteOff){
                                                //核销订单
                                                logFile("ych 核销订单")
                                                targetOrder = mModel?.getWriteOffOrder(ychOrder.orderNumber)
                                            }else if (isCash){
                                                //现金订单
                                                logFile("ych 现金订单")
                                                targetOrder = mModel?.checkOrder(ychOrder, isCash)
                                                //重新赋值
                                                money = targetOrder?.money?:"0"
                                                coins = targetOrder?.amount?:"0"
                                            } else{
                                                //反扫付款码支付订单
                                                logFile("ych 反扫付款码支付订单")
//                                                targetOrder = mModel?.checkOrder(ychOrder, isCash)
//                                                targetOrder = RoomUtil.orderGetByOrderNumber(ychOrder.orderNumber)?.get(0)
                                                targetOrder = RoomUtil.orderGetByPayUUID(ychOrder.payUuid)?.get(0)
                                                //重新赋值
                                                money = targetOrder?.money?:"0"
                                                coins = targetOrder?.amount?:"0"
                                            }
                                            logFile("ych校验后的订单数据：${GsonUtil.getInstance()?.toJson(targetOrder)}")
                                        }
                                    }

                                    BackstagePlatform.KKB -> {
                                        val kkbOrder = OrderEntity().apply {
                                            uuid = k
                                            payUuid = k
                                            platform = p
                                            amount = coins
                                            this.money = money
                                            this.orderNumber = orderNumber
                                            isPay = PayState.SUCCESS
                                        }
                                        logFile("kkb校验订单数据：${GsonUtil.getInstance()?.toJson(kkbOrder)}")
                                        withContext(Dispatchers.IO){
                                            if (isMqOrderYch && !isWithdrawal && !isWriteOff){
                                                //普通订单
                                                logFile("kkb 普通订单")
                                                mModel?.saveOrder(kkbOrder)
                                                targetOrder = kkbOrder
                                            }else if (isWithdrawal){
                                                //提币订单
                                                logFile("kkb 提币订单")
                                                targetOrder = mModel?.getWithdrawalOrder(kkbOrder.orderNumber)?.apply {
                                                    //提交本地提币订单到后台
//                                                    mModel?.placeLocalWithdrawalOrder(
//                                                        p,
//                                                        k,
//                                                        true,
//                                                        money,
//                                                        coins.toLong(),
//                                                        coins.toLong(),
//                                                        isMqOrderYch,
//                                                        isWriteOff,
//                                                        isWithdrawal,
//                                                        isCash,
//                                                        uid)?.let {
//                                                        mView?.placeLocalWithdrawalOrder(it)
//                                                    }
                                                }
                                            }else if (isWriteOff){
                                                //核销订单
                                                logFile("kkb 核销订单")
                                                targetOrder = mModel?.getWriteOffOrder(kkbOrder.orderNumber)
                                            }else if (isCash){
                                                //现金订单
                                                logFile("kkb 现金订单")
                                                targetOrder = mModel?.checkOrder(kkbOrder, isCash)
                                                //重新赋值
                                                money = targetOrder?.money?:"0"
                                                coins = targetOrder?.amount?:"0"
                                            } else{
                                                //反扫付款码支付订单
                                                logFile("kkb 反扫付款码支付订单")
//                                                targetOrder = mModel?.checkOrder(ychOrder, isCash)
//                                                targetOrder = RoomUtil.orderGetByOrderNumber(ychOrder.orderNumber)?.get(0)
                                                targetOrder = RoomUtil.orderGetByPayUUID(kkbOrder.payUuid)?.get(0)
                                                //重新赋值
                                                money = targetOrder?.money?:"0"
                                                coins = targetOrder?.amount?:"0"
                                            }
                                            logFile("kkb校验后的订单数据：${GsonUtil.getInstance()?.toJson(targetOrder)}")
                                        }
                                    }
                                }

                                if (targetOrder == null) {
                                    withContext(Dispatchers.IO) {
                                        //上报出币失败
                                        val result = mModel?.postResult(p, k, false, money, currentOutCoins.toLong(), coins.toLong(), isMqOrderYch, isWriteOff, isWithdrawal, isCash, uid)
                                        mView?.postResult(result)
                                    }
                                    isHandling = false
                                    delay(1000)
                                    logFile("订单处理结束：校验订单为null==========")
                                    return@checkSize
                                }

                                //更新订单状态
                                targetOrder!!.isPay = PayState.SUCCESS
                                withContext(Dispatchers.IO){
                                    RoomUtil.orderUpdateByUuid(targetOrder!!.uuid, targetOrder!!.isPay)
                                }

                                val orderCoins = targetOrder!!.amount
                                val uuid = targetOrder!!.uuid
                                withContext(Dispatchers.Main) {
                                    //显示出币窗口
                                    mView?.showCoinsInfo(uuid, orderCoins, true, false)
                                }

                                outCoinsState = OutCoinState.WAITING

                                coinsResponTimeout = 18
                                while (coinsResponTimeout > 0){
                                    delay(1000)
                                    when (outCoinsState){
                                        OutCoinState.WAITING -> {
                                            coinsResponTimeout --
                                        }
                                        OutCoinState.FINISH -> {
                                            coinsResponTimeout = 0
                                        }
                                        OutCoinState.ERR -> {
                                            coinsResponTimeout = 0
                                        }
                                    }
                                    logFile("coinsResponTimeout=$coinsResponTimeout")
                                }
                                coinsResponTimeout = 18
                                var result = true
                                when (outCoinsState){
                                    OutCoinState.WAITING -> {
                                        logFile("发送出币指令18秒无响应，上报启动异常")
                                        outCoinsState = OutCoinState.ERR
                                        targetOrder?.let {
                                            it.isPay = PayState.TIMEOUT
                                        }
                                        result = false
                                    }
                                    OutCoinState.FINISH -> {
                                        logFile("出币完成，上报出币结果")
                                        targetOrder?.let {
                                            it.isPay = PayState.COMPLETE
                                        }
                                        result = true
                                    }
                                    OutCoinState.ERR -> {
                                        logFile("出币结束，出币异常")
                                        //更新订单状态
                                        targetOrder?.let {
                                            it.isPay = PayState.COMPLETE
                                        }
                                        result = false
                                    }
                                }

                                //出币结果异常，需要更新设备状态
                                setDeviceStatus(currentOutCoins == orderCoins)

                                targetOrder?.let {
                                    withContext(Dispatchers.IO){
                                        it.realAmount = currentOutCoins
                                        mModel?.updateOrder(it)
                                    }
                                }
                                outCoinsState = OutCoinState.UNSEND

                                if (isCash && p == BackstagePlatform.YCH_SDK){
                                    extRepData?.let {
                                        val ychCashData = extRepData as RepCashYch
                                        ychCashData.count = currentOutCoins.toLong()
                                        ychCashData.outStatus = if (result) 0 else 1
                                        mView?.uploadCashData(ychCashData)
                                    }
                                }else {
                                    val reqData = mModel?.postResult(
                                        p,
                                        k,
                                        result,
                                        money,
                                        currentOutCoins.toLong(),
                                        coins.toLong(),
                                        isMqOrderYch,
                                        isWriteOff,
                                        isWithdrawal,
                                        isCash,
                                        uid
                                    )
                                    mView?.postResult(reqData)
                                }

                                isHandling = false
                                delay(1000)
                                logFile("订单处理结束：完成==========")
                                return@checkSize
                            }
                        }
                    }
                    delay(1000)
                } catch (e: Exception) {
                    e.printStackTrace()
                    logFile("handleJob异常：${e.message}")
                    isHandling = false
                }
            }
        }

    }

    override fun recieveOrder(platform: BackstagePlatform, extRepData: Any?, orderData: Any) {
        orderQueue.add(OrderDataBean(platform, extRepData, orderData, "", false))
    }

    override fun saveGoods(platform: BackstagePlatform, dataList: Any) {
        lifecycleCoroutineScope.launch(Dispatchers.Main) {
            var lastGood: GoodListBean? = null
            withContext(Dispatchers.IO) {
                lastGood = mModel?.saveGoods(platform, dataList)
            }
            mView?.refreshDatas(platform, lastGood)
        }
    }

    override fun getPayCode(goodBean: GoodBean, payTypeData: PayTypeData?) {
        lifecycleCoroutineScope.launch(Dispatchers.Main) {
            var requestData: ReqPayCodeBean? = null
            withContext(Dispatchers.IO) {
                requestData = mModel?.getPayCode(goodBean, payTypeData)
            }
            mView?.getPayCode(requestData)
        }
    }

    override fun setOrderTimeout(k: String) {
        lifecycleCoroutineScope.launch {
            withContext(Dispatchers.IO){
                mModel?.setOrderTimeout(k)
            }
        }
    }

    override fun coinResponse(finish: Boolean, success: Boolean, coins: String) {
        currentOutCoins = coins
        if (finish) {
            outCoinsState = if (success) OutCoinState.FINISH else OutCoinState.ERR
        } else {
            coinsResponTimeout = 30
        }
    }

    override fun outCoins(coins: Long) {
        lifecycleCoroutineScope.launch {
            withContext(Dispatchers.IO){
                mModel?.outCoins(coins)
            }
        }
    }

    override fun clearSelectGood() {
        mModel?.clearSelectGood()
    }

    override fun postBackscanData(type: Int, scanData: String, goodBean: GoodBean?, scanDataType: String) {
        lifecycleCoroutineScope.launch {
            var requestData: ReqBackscanPayBean? = null
            withContext(Dispatchers.IO) {
                requestData = mModel?.postBackscanData(type, scanData, goodBean, scanDataType)
            }
            mView?.postBackscanPay(requestData)
        }
    }

    override fun getCashGoods(platform: BackstagePlatform) {
        lifecycleCoroutineScope.launch {
            var cashGoods: MutableList<CashGoodBean>? = null
            var lastOrder: OrderEntity? = null
            withContext(Dispatchers.IO) {
                cashGoods = mModel?.getCashGoods(platform)
                lastOrder = mModel?.getLastCashOrder(platform)?.let {
                    if (it.isEmpty()){
                        null
                    }else{
                        it[0]
                    }
                }
                //
                mModel?.setCurrentCashOrder(lastOrder)
            }
            mView?.showCashGoods(cashGoods, lastOrder)
        }
    }

    override fun cashPayOut(platform: BackstagePlatform, money: String, coins: String) {
        lifecycleCoroutineScope.launch {
            var repCashDataBean: RepCashDataBean? = null
            withContext(Dispatchers.IO) {
                repCashDataBean = mModel?.cashPayOut(platform, money, coins)
            }
            repCashDataBean?.let {
                if (platform == BackstagePlatform.WHM_MQTT_HEX) {
                    mView?.uploadCashData(it.repData)
                }
//                if (platform == BackstagePlatform.KKB) {
//                    mView?.placeCashOrder(money, coins, it.uuid)
//                }
                recieveOrder(platform, it.repData, it.orderData)
            }
        }
    }

    override fun localWithdrawalOut(platform: BackstagePlatform, uid: String, coins: String) {
        lifecycleCoroutineScope.launch {
            var localWithdrawalOrderBean: LocalWithdrawalOrderBean? = null
            withContext(Dispatchers.IO) {
                localWithdrawalOrderBean = mModel?.localWithdrawalOut(platform, uid, coins)
            }
            localWithdrawalOrderBean?.let { bean ->
                bean.orderData?.let { orderQueue.add(OrderDataBean(platform, null, it, uid, true)) }
            }
        }
    }

    override fun getLastCashOrder() {

    }

    override fun saveMoney(checkMoney: Long, totalMoney: Long) {
        lifecycleCoroutineScope.launch {
            val result = mModel?.saveMoney(checkMoney, totalMoney)
            result?.let {
                mView?.saveMoneyFinish(it.orderNumber, it.totalMoney, it.coins)
//                it.repData?.let { cashData ->
//                    if (cashData.platform == BackstagePlatform.WHM_MQTT_HEX) {
//                        mView?.uploadCashData(cashData.repData)
//                    }
//                    if (it.canOut){
//                        recieveOrder(cashData.platform, cashData.repData, cashData.orderData)
//                    }
//                }
            }
        }
    }

    override fun loadCoins() {
        lifecycleCoroutineScope.launch {
            val coins = withContext(Dispatchers.IO) {
                mModel?.loadCoins()?: "0"
            }
            mView?.refreshRemainCoins(coins)
        }
    }

    override fun refreshPayCode(data: String) {
        lifecycleCoroutineScope.launch(Dispatchers.Main) {
            val codeData = withContext(Dispatchers.IO) {
                mModel?.refreshPayCode(data)?: ""
            }
            mView?.refreshPayCode(codeData)
        }
    }

    override fun insertSaveCoin(userInfo: Any) {
        lifecycleCoroutineScope.launch {
            val loginBean = withContext(Dispatchers.IO) {
                mModel?.insertSaveCoin(userInfo)
            }
            mView?.saveCoinLogin(loginBean)
        }
    }

    override fun refreshSaveCoins(orderNumber: String, coins: String) {
        lifecycleCoroutineScope.launch {
            withContext(Dispatchers.IO) {
                mModel?.refreshSaveCoins(orderNumber, coins)
            }
        }
    }

    override fun checkSaveCoins(orderNumber: String, userId: String, transId: String, coins: String) {
        lifecycleCoroutineScope.launch {
            val result = withContext(Dispatchers.IO) {
                mModel?.checkSaveCoins(orderNumber, userId, transId, coins)?: CheckSaveCoinsResultBean(false, null)
            }
            mView?.checkSaveCoinsResult(result)
        }
    }

    override fun recieveWithdrawalOrder(platform: BackstagePlatform, orderData: Any) {
        orderQueue.add(OrderDataBean(platform, null, orderData, isWithdrawal = true))
    }

    override fun getWithdrawalCode(platform: BackstagePlatform) {
        lifecycleCoroutineScope.launch {
            val result = withContext(Dispatchers.IO) {
                mModel?.getWithdrawalCode(platform)
            }
            result?.let {
                mView?.getWithdrawalCode(it)
            }
        }
    }

    override fun getMultCouponChannel(platform: BackstagePlatform) {
        lifecycleCoroutineScope.launch {
            val result = withContext(Dispatchers.IO) {
                mModel?.getMultCouponChannel(platform)
            }
            result?.let {
                mView?.getMultCouponChannel(it)
            }
        }
    }

    override fun repWiteOffData(platform: BackstagePlatform, channel: String, codeData: String) {
        lifecycleCoroutineScope.launch {
            val result = withContext(Dispatchers.IO) {
                mModel?.repWiteOffData(platform, channel, codeData)
            }
            result?.let {
                mView?.repWiteOffData(it)
            }
        }
    }

    override fun recieveWriteOffOrder(platform: BackstagePlatform, orderData: Any) {
        orderQueue.add(OrderDataBean(platform, null, orderData, isWithdrawal = false))
    }

    override fun placeLocalWithdrawalOrder(
        platform: BackstagePlatform,
        uid: String,
        quantity: Int,
    ) {
        lifecycleCoroutineScope.launch {
            withContext(Dispatchers.IO) {
                mModel?.placeLocalWithdrawalOrder(
                    platform,
                    CommonUtil.getUuid().replace("-", ""),
                    true,
                    "0",
                    quantity.toLong(),
                    quantity.toLong(),
                    false,
                    false,
                    true,
                    false,
                    uid
                )?.let {
                    mView?.placeLocalWithdrawalOrder(it)
                }
            }

        }
    }

    override fun setSaveStatus(platform: BackstagePlatform, saving: Boolean) {
        if (saving){
            //创建存币订单
            orderQueue.add(OrderDataBean(platform, null, "", "", false, true))
        }else{
            //存币结束
            isSaving = false
        }
    }

    override fun dispenserOrder(platform: BackstagePlatform, repCashDataBean: RepCashDataBean) {
        if (platform == BackstagePlatform.WHM_MQTT_HEX) {
            mView?.uploadCashData(repCashDataBean.repData)
        }
        recieveOrder(platform, repCashDataBean.repData, repCashDataBean.orderData)
    }

    override fun getHandling(): Boolean {
        return !isHandling && orderQueue.size == 0
    }

    override fun detechView() {
        super.detechView()
        isLoop = false
        handleJob?.cancel()
    }

}